package org.jeecg.modules.api.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.api.util.FastJsonUtils;
import org.jeecg.modules.api.util.HttpClient;
import org.jeecg.modules.order.entity.Order;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

import static org.jeecg.modules.api.util.Constants.*;

/**
 * @author: Dean
 * @since: 2019/9/28
 */
@Service
@Slf4j
public class TspRemoteService {


    public TspOrderResponse createOrder(Order order) {
        TspOrderCreateForm tspOrderCreateForm = TspOrderCreateForm.builder()
                .merchantOrderNo(order.getOrderNo())
                .amount(getAmount(order.getAmount()))
                .payProduct(StringUtils.isEmpty(order.getGoodsDesc())?"商品":order.getGoodsDesc())
                .currency(TSP_CUREENCY).build();
        HttpClient HttpClient = new HttpClient();
        log.info("TSP订单创建开始");
        String s = HttpClient.createStream(TSP_ORDER_CREATE_URL, tspOrderCreateForm);
        log.info("TSP订单创建返回[{}]", s);
        TspOrderResponse tspOrderResponse = (TspOrderResponse) FastJsonUtils.convertJsonToObject(s, TspOrderResponse.class);
        log.info("TSP订单创建封装[{}]", tspOrderResponse.toString());
        return tspOrderResponse;
    }

    public TspCollectCodeResponse collectCodeGenerate(String payOrderNo){
        TspCollectCodeForm tspCollectCodeForm = TspCollectCodeForm.builder()
                .payOrderNo(payOrderNo)
                .build();
        HttpClient HttpClient = new HttpClient();
        log.info("TSP支付码创建开始");
        String s = HttpClient.createStream(TSP_COLLECTCODE_GENERATE_URL, tspCollectCodeForm);
        log.info("TSP支付码创建返回[{}]", s);
        TspCollectCodeResponse tspCollectCodeResponse = (TspCollectCodeResponse) FastJsonUtils.convertJsonToObject(s, TspCollectCodeResponse.class);
        log.info("TSP支付码创建封装[{}]", tspCollectCodeResponse.toString());
        return tspCollectCodeResponse;
    }

    public TspCompleteOrderResponse completeOrder(String payOrderNo,String payCode){
        TspCompleteOrderForm tspCompleteOrderForm = TspCompleteOrderForm.builder()
                .payOrderNo(payOrderNo)
                .payCode(payCode)
                .build();
        HttpClient HttpClient = new HttpClient();
        log.info("TSP完成支付请求开始");
        String s = HttpClient.createStream(TSP_COMPLETE_ORDER_URL, tspCompleteOrderForm);
        log.info("TSP完成支付请求返回[{}]", s);
        TspCompleteOrderResponse tspCompleteOrderResponse = (TspCompleteOrderResponse) FastJsonUtils.convertJsonToObject(s, TspCompleteOrderResponse.class);
        log.info("TSP完成支付请求创建封装[{}]", tspCompleteOrderResponse.toString());
        return tspCompleteOrderResponse;
    }

    public TspOrderStatusResponse getOrderStatus(String payOrderNo){
        TspOrderStatusQuery tspOrderStatusQuery = TspOrderStatusQuery.builder()
                .payOrderNo(payOrderNo)
                .build();
        HttpClient HttpClient = new HttpClient();
        log.info("TSP订单查看请求开始");
        String s = HttpClient.createStream(TSP_ORDER_STATUS_URL, tspOrderStatusQuery);
        log.info("TSP完成订单查看请求返回[{}]", s);
        TspOrderStatusResponse tspOrderStatusResponse = (TspOrderStatusResponse) FastJsonUtils.convertJsonToObject(s, TspOrderStatusResponse.class);
        log.info("TSP完成订单查看请求创建封装[{}]", tspOrderStatusResponse.toString());
        return tspOrderStatusResponse;
    }

    private Double getAmount(Long fee){
        Double d = fee*0.01/10;
        BigDecimal b = new BigDecimal(d);
        d = b.setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
        return d;
    }
}
